package com.cnswhy.cloud.server.merchant.controller.shop;

import com.cnswhy.cloud.common.redis.service.IdGeneratorService;
import com.cnswhy.cloud.server.core.entity.ShopBrand;
import com.cnswhy.cloud.server.core.entity.ShopClassify;
import com.cnswhy.cloud.server.core.entity.ShopProduct;
import com.cnswhy.cloud.server.core.entity.ShopProductGallery;
import com.cnswhy.cloud.server.core.dto.ShopProductGallerySaveDTO;
import com.cnswhy.cloud.server.core.dto.ShopProductGalleryUpdateDTO;
import com.cnswhy.cloud.server.core.dto.ShopProductGalleryPageDTO;
import com.cnswhy.cloud.server.core.service.ShopClassifyService;
import com.cnswhy.cloud.server.core.service.ShopProductGalleryService;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.server.core.service.shop.ShopProductService;
import com.cnswhy.cloud.server.merchant.util.OssUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
//import com.github.zuihou.security.annotation.PreAuth;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 商品相册(t_shop_product_gallery)
 * </p>
 *
 * @author cnswhy
 * @date 2020-11-17
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/shopProductGallery")
@Api(value = "ShopProductGallery", tags = "商品相册(t_shop_product_gallery)")
//@PreAuth(replace = "shopProductGallery:")
public class ShopProductGalleryController extends SuperController<ShopProductGalleryService, Long, ShopProductGallery, ShopProductGalleryPageDTO, ShopProductGallerySaveDTO, ShopProductGalleryUpdateDTO> {
    @Autowired
    ShopProductService shopProductService;
    @Autowired
    IdGeneratorService idGeneratorService;
    @Autowired
    ShopClassifyService classifyService;
    @PostMapping("/syncUrl")
    public R<Boolean> syncUrl() {
        List<ShopClassify> shopClassifies = classifyService.list();
        for(ShopClassify shopClassify : shopClassifies){
            if(StringUtils.isBlank(shopClassify.getUrl())){
                try{
                    String url = OssUtils.updateLoadFileToOss("", "http://139.9.69.116:81/" + shopClassify.getImgUrl(),idGeneratorService.genCommonIdString() + ".jpg");
                    shopClassify.setUrl(url);
                    classifyService.updateById(shopClassify);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        /*List<ShopProduct> shopProducts = shopProductService.list();
        for(ShopProduct shopProduct : shopProducts){
            try{
                String url = OssUtils.updateLoadFileToOss("shopBrand", "http://139.9.69.116:81/" + shopProduct.getThumbnailImg(),idGeneratorService.genCommonIdString() + ".jpg");
                shopProduct.setUrl(url);
                url = OssUtils.updateLoadFileToOss("shopBrand", "http://139.9.69.116:81/" + shopProduct.getPosterImg(),idGeneratorService.genCommonIdString() + ".jpg");
                shopProduct.setPosterUrl(url);
                shopProductService.updateById(shopProduct);
            }catch (Exception e){

            }
        }*/
       /* for(ShopProduct shopProduct : shopProducts){
            try {
                String sliderImgs = shopProduct.getSliderImgs();
                String[] res = sliderImgs.split(",");
                int sort = 0;
                for(var abc: res){
                    String url = OssUtils.updateLoadFileToOss("shopBrand", "http://139.9.69.116:81/" + abc,idGeneratorService.genCommonIdString() + ".jpg");
                    ShopProductGallery shopProductGallery = new ShopProductGallery();
                    sort ++;
                    shopProductGallery.setProductId(shopProduct.getId());
                    shopProductGallery.setOriginal(url);
                    shopProductGallery.setIsDefailt(0);
                    shopProductGallery.setSort(sort);
                    baseService.save(shopProductGallery);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }*/
        return success();
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ShopProductGallery> shopProductGalleryList = list.stream().map((map) -> {
            ShopProductGallery shopProductGallery = ShopProductGallery.builder().build();
            return shopProductGallery;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(shopProductGalleryList));
    }
}
